package com.whsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whsxt.domain.UserCollection;
import com.whsxt.es.ProdEs;
import com.whsxt.feign.UserCollectProdFeign;
import com.whsxt.mapper.UserCollectionMapper;
import com.whsxt.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-21 16:50
 * @Description: ${Description}
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {


    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Autowired
    private UserCollectProdFeign userCollectProdFeign;


    /**
     * 分页查询收藏商品
     *
     * @param page
     * @param openId
     * @return
     */
    @Override
    public IPage<ProdEs> findUserCollectProd(Page<UserCollection> page, String openId) {
        //分页查询收藏表，
        Page<UserCollection> userCollectionPage = userCollectionMapper.selectPage(page, new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, openId)
        );

        //new一个最终的分页对象
        Page<ProdEs> prodEsPage = new Page<>(page.getCurrent(), page.getSize());

        List<UserCollection> userCollectionList = userCollectionPage.getRecords();
        if (CollectionUtils.isEmpty(userCollectionList)) {
            return prodEsPage;
        }

        //拿到商品的id集合
        List<Long> proIds = userCollectionList.stream().map(UserCollection::getProdId).collect(Collectors.toList());
        //查询es，远程调用
        List<ProdEs> prodEsList = userCollectProdFeign.findProdEsByIds(proIds);
        prodEsPage.setRecords(prodEsList);
        prodEsPage.setTotal(userCollectionPage.getTotal());
        return prodEsPage;
    }


    /**
     * 查询用户是否收藏该商品
     *
     * @param prodId
     * @param openId
     * @return
     */
    @Override
    public boolean findUserIsCollection(Long prodId, String openId) {
        Integer count = userCollectionMapper.selectCount(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, openId)
                .eq(UserCollection::getProdId, prodId)
        );
        return count > 0;
    }


    /**
     * 用户添加或者取消收藏
     *
     * @param prodId
     * @param openId
     */
    @Override
    public void addOrCancel(Long prodId, String openId) {
        //查询这个商品之前是否被这个用户收藏过
        UserCollection userCollection = userCollectionMapper.selectOne(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, openId)
                .eq(UserCollection::getProdId, prodId)
        );
        if (userCollection == null) {
            //收藏
            UserCollection collection = new UserCollection();
            collection.setCreateTime(new Date());
            collection.setProdId(prodId);
            collection.setUserId(openId);
            userCollectionMapper.insert(collection);
        } else {
            //取消
            userCollectionMapper.deleteById(userCollection.getId());
        }
    }
}
